
out = list()

for(cy in c(2012, 2016)){
  out[[as.character(cy)]] = open_dataset(glue('df{substr(cy, 3, 4)}.parquet')) %>%
    group_by(nat_quant = case_when(
      as.numeric(as.character(nat_quant)) < 0.9 ~ 'Bottom 90%',
      as.numeric(as.character(nat_quant)) < 0.99 ~ '90% - 99%',
      as.numeric(as.character(nat_quant)) < 0.999 ~ '99% - 99.9%',
      T ~ 'Top 0.1%'
    )) %>%
    summarise(summwealth=sum(total)) %>%
    collect()
}

out = rbindlist(out, idcol = 'cycle')

out[, summwealth := summwealth/sum(summwealth), cycle]

szz = tribble(
  ~ cycle, ~ nat_quant, ~ summwealth,
  2012, "Bottom 90%", 0.2904575,
  2012, "90% - 99%", 0.450616, 
  2012, "99% - 99.9%", 0.1806852,
  2012, "Top 0.1%", 0.07824128,
  2016, "Bottom 90%", 0.3327100,
  2016, "90% - 99%", 0.4217171, 
  2016, "99% - 99.9%", 0.1716373,
  2016, "Top 0.1%", 0.07393559
) %>% as.data.table()

out = rbindlist(list(out[, class := 'Our Estimate'], szz[, class := 'SZZ']))

out[, per_cap := fcase(
  nat_quant == "Bottom 90%", summwealth / 900,
  nat_quant == "90% - 99%", summwealth / 90,
  nat_quant == "99% - 99.9%", summwealth / 9,
  default = summwealth
)]

out[, log_per_cap := log(per_cap)]

out[, nat_quant := fct_relevel(nat_quant, c('Bottom 90%', '90% - 99%', '99% - 99.9%', 'Top 0.1%'))]

save(out, file = 'summary_data/figA5.rda')
